package example.binarylight;

import org.fourthline.cling.binding.annotations.*;
import java.beans.PropertyChangeSupport;

@UpnpService(serviceId = @UpnpServiceId("SwitchPower"), serviceType = @UpnpServiceType(value = "SwitchPower", version = 1))
public class SwitchPower {


    @UpnpStateVariable(defaultValue = "0", sendEvents = false)
    private boolean target = false;

    @UpnpStateVariable(defaultValue = "0")
    private boolean status = false;

    @UpnpStateVariable(defaultValue = "")
    private String info = "";

    private final PropertyChangeSupport propertyChangeSupport;


    public SwitchPower() {
        this.propertyChangeSupport = new PropertyChangeSupport(this);
    }

    public PropertyChangeSupport getPropertyChangeSupport() {
        return propertyChangeSupport;
    }

    @UpnpAction
    public void setInfo(@UpnpInputArgument(name = "NewInfo") String newInfo) {
    	String infoOldValue = info;
        info = newInfo;
        System.out.println("======Info: " + newInfo);

        getPropertyChangeSupport().firePropertyChange("Info", infoOldValue, info);
    }

    @UpnpAction(out = @UpnpOutputArgument(name = "RetInfo"))
    public String getInfo() {
        return info;
    }

    @UpnpAction
    public void setTarget(@UpnpInputArgument(name = "NewTargetValue") boolean newTargetValue) {
        //target = newTargetValue;
        //status = newTargetValue;
		
		boolean targetOldValue = target;
        target = newTargetValue;
        boolean statusOldValue = status;
        status = newTargetValue;
		
        System.out.println("======Switch is: " + status);
		
		// These have no effect on the UPnP monitoring but it's JavaBean compliant
        getPropertyChangeSupport().firePropertyChange("target", targetOldValue, target);
        getPropertyChangeSupport().firePropertyChange("status", statusOldValue, status);

        // This will send a UPnP event, it's the name of a state variable that triggers events
        getPropertyChangeSupport().firePropertyChange("Status", statusOldValue, status);
    }

    @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
    public boolean getTarget() {
        return target;
    }

    @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
    public boolean getStatus() {
        // If you want to pass extra UPnP information on error:
        // throw new ActionException(ErrorCode.ACTION_NOT_AUTHORIZED);
        return status;
    }







    //===============================================
    @UpnpAction
    public void setValues(  @UpnpInputArgument(name="Value1", stateVariable="Info") String value1
    	                  , @UpnpInputArgument(name="Value2", stateVariable="Info") String value2
    	                  , @UpnpInputArgument(name="Value3", stateVariable="Info") String value3
    	                 ) {
    	System.out.println("======setValues: " + value1 + " " + value2 + " " + value3);
    }


    @UpnpAction(out = @UpnpOutputArgument(name = "RetStr", stateVariable="Info"))
    public String testGetStr(@UpnpInputArgument(name="Str", stateVariable="Status")boolean b) {
    	if (b) {
    		return "AAAA";
    	} else {
    		return "BBBB";
    	}
    }

}